package com.melodyframe.stage.web.context;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.melodyframe.common.context.ScopeContext;

public class RequestContext implements ScopeContext {
	private HttpServletRequest		request;
	private HttpServletResponse		response;
	private Map<String, String[]>	parameterMap;

	@Override
	public boolean containsKey(Object key) {
		return parameterMap.containsKey(key)
				|| null != request.getAttribute(key.toString());
	}

	@Override
	public Object get(Object key) {
		if (parameterMap.containsKey(key)) {
			String[] values = parameterMap.get(key);
			if (values.length == 1) {
				return values[0];
			} else {
				return values;
			}
		} else {
			return request.getAttribute(key.toString());
		}
	}

	@Override
	public Object put(String key, Object value) {
		request.setAttribute(key, value);
		return value;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		this.parameterMap = this.request.getParameterMap();
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

}
